import { Body, Controller, Post,Get, Req } from '@nestjs/common';
import { GatherParameterService } from './parameter.service';
import { ADMIN_PREFIX } from '../../admin.constants';
import {
  ApiOkResponse,
  ApiOperation,
  ApiSecurity,
  ApiTags, 
} from '@nestjs/swagger';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('采集管理模块')
@Controller('parameter')
export class GatherParameterController {
  constructor(private gatherParameterService: GatherParameterService) {}

  @ApiOperation({ summary: '获取采集列表' })
  @ApiOkResponse()
  @Get('list')
  async list() {
    return await this.gatherParameterService.list();
  }

  @ApiOperation({ summary: '分页获取获取采集列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.gatherParameterService.page(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加采集' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.gatherParameterService.isExistKey(dto.parameterCode);

    await this.gatherParameterService.add(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '更新采集' })
  @ApiOkResponse()
  @Post('update')
  async update(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.gatherParameterService.update(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除采集' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.gatherParameterService.isExistKey2(dto);

    await this.gatherParameterService.delete(dto, user, mac, ip, syncHostSqlite);
  }
}
